﻿#include "PropertyView.h"
#include <QPoint>
#include <QMenu>
#include <QMessageBox>
#include <QHeaderView>
#include <QIcon>
#include <QColor>
#include <QDateTime>
#include <QPalette>
#include <QComboBox>
#include <QStringList>
#include <QItemDelegate>
#include <QSize>
#include <QItemSelectionModel>
#include "PropDelegate.h"
#include "SetColValDlg.h"
#include <QTableWidgetSelectionRange>
#include <QList>
#include <QScrollBar>
#include <QAbstractButton>
#include <QPalette>
#include <QClipboard>
#include <QMimeData>
#include <QUrl>


PropertyView::PropertyView(MyTableData *pTableData, QWidget *parent):QTableWidget(parent)
{
//	m_CurRecordNo=0;
//	m_CurRecordId=0;
//	m_bModified = XFALSE;
// 	m_LastSelRowNum = 0;
// 	m_LastSelItemRowNum = 0;
// 	m_LastSelItemColNum = 0;
//	m_LastbSetColFlag = false;
	m_pTableData = NULL;
		
	m_pTableData = pTableData;
	QColor	   tempcolor;
	QPalette   oldPalette = this->palette();
	tempcolor = oldPalette.color(QPalette::Highlight);
	oldPalette.setColor(QPalette::Highlight, tempcolor);
	oldPalette.setColor(QPalette::HighlightedText, Qt::white);
	setPalette(oldPalette);

	setCornerButtonEnabled(false);
	verticalHeader()->setResizeMode(QHeaderView::Fixed);  
	if (!alternatingRowColors())
	{
		setAlternatingRowColors(true);
	}
	setSelectionMode(QAbstractItemView::SingleSelection);
	setSelectionBehavior(QAbstractItemView::SelectRows);
	PropDelegate *pPropDelegate = new PropDelegate(m_pTableData, this);             //创建一个自定义的代理类
	if (pPropDelegate)
	{
		setItemDelegate(pPropDelegate);
		//connect(pPropDelegate, SIGNAL(Signal_RefreshShow(int, int, int,int)), this, SLOT(OnReSetDataShow(int, int, int,int)));	
		connect(pPropDelegate, SIGNAL(Signal_UserDataChang(int, bool)), this, SLOT(OnItemUserDataChang(int,bool)));
	}
	
	InitHeaderItem();
//	connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(SelectChanged()));
	//jwj 2014-05-21
//	setupPopMenu();
	//setHorizontalScrollMode(ScrollPerPixel);
}

PropertyView::~PropertyView()
{
	//m_pTableData->ReleaseTableData();
}

void PropertyView::ClearAllShowCont()
{
	while (rowCount()>0)
	{
		removeRow(0);
	}
	
}

//	初始化表头显示
int PropertyView::InitHeaderItem()
{
	QTableWidgetItem *HeadItem = NULL;
	QString HeadItemText;
	//QString HeadIcon;
	setColumnCount(2);
	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("属性"));
	//HeadItem->setIcon(QIcon(HeadIcon));
	HeadItem->setTextAlignment(Qt::AlignCenter);
	setHorizontalHeaderItem(0, HeadItem);
	setColumnWidth(0, 100);

	HeadItem = new QTableWidgetItem;
	HeadItem->setText(QString::fromLocal8Bit("值"));
	//HeadItem->setIcon(QIcon(HeadIcon));
	HeadItem->setTextAlignment(Qt::AlignCenter);
	setHorizontalHeaderItem(1, HeadItem);
	setColumnWidth(1, 100);

	return 1;
}

//	初始化属性标题
int PropertyView::InitPropertyTitle()
{
	if (!m_pTableData->IsRecordParamSafe())
	{
		return -1;
	}
	clearContents();
	
	int				 RowNum = 0;
	int				 ColWidth=0;
	QTableWidgetItem *pVHeadItem=NULL;
	QTableWidgetItem *pPropTitleItem=NULL;
	QTableWidgetItem *pValueItem=NULL;
	QString			 ItemTextStr;
	RowNum = m_pTableData->GetCurShowFieldNum();
	if (RowNum <= 0)
	{
		return -2;
	}
	
	setRowCount(RowNum);
	
	for (int RowNo=0; RowNo<RowNum; RowNo++)
	{
		pVHeadItem = NULL;
		pVHeadItem = verticalHeaderItem(RowNo);
		if (!pVHeadItem)
		{
			pVHeadItem = new QTableWidgetItem;
		}

		SetVerticalHeadShow(RowNo, pVHeadItem);
		pPropTitleItem = NULL;
		pPropTitleItem = item(RowNo, 0);
		if (!pPropTitleItem)
		{
			pPropTitleItem = new QTableWidgetItem;
			setItem(RowNo, 0, pPropTitleItem);
		}
		m_pTableData->GetHeadText(RowNo, ItemTextStr, ColWidth);
		pPropTitleItem->setText(ItemTextStr);
		pPropTitleItem->setData(Qt::ToolTipRole, ItemTextStr);
		//pPropTitleItem->setData(Qt::UserRole, ItemVarData);
		pPropTitleItem->setData(Qt::WhatsThisRole, ItemTextStr);
		pPropTitleItem->setFlags(Qt::ItemIsSelectable | 
								Qt::ItemIsDragEnabled |
								Qt::ItemIsEnabled |
								Qt::ItemIsTristate);
		pPropTitleItem->setBackgroundColor(QColor(205, 205, 205));

		pValueItem = NULL;
		pValueItem = item(RowNo, 1);
		if (!pValueItem)
		{
			pValueItem = new QTableWidgetItem;
			setItem(RowNo, 1, pValueItem);
		}
		
		pValueItem->setFlags(Qt::ItemIsSelectable |
							 Qt::ItemIsDragEnabled |
							 Qt::ItemIsEnabled |
							 Qt::ItemIsTristate);
	}
	
	return 1;
}

// 设置行标头显示
int PropertyView::SetVerticalHeadShow(int RowNo, QTableWidgetItem *pVHeadItem)
{
	if (pVHeadItem)
	{
		QString				ItemText;
		ItemText= QString("%1").arg(RowNo+1);	
		pVHeadItem->setText(ItemText);
		pVHeadItem->setTextAlignment(Qt::AlignLeft|Qt::AlignVCenter);
		pVHeadItem->setSizeHint(QSize(20, 20));
		pVHeadItem->setData(Qt::UserRole, QVariant(RowNo+1));
		setVerticalHeaderItem(RowNo, pVHeadItem);		
		return 1;
	}
	return 0;
}

//响应主工作区选择显示某条记录所以字段
void PropertyView::OnShowRecord(int RecordNo)
{
	ClearTableDataShow();
	if (m_pTableData->SetCurSelectRecordNo(RecordNo)<0)
	{
		return ;
	}
	OneRowTableDataShow();
}

void PropertyView::ClearTableDataShow()//ccg 清空属性区域表格
{
	int rowcount = rowCount();
	if (rowcount<=0)
	{
		return ;
	}

	QTableWidgetItem	*pItem = NULL;
	
	for (int j=0; j<rowcount; j++)
	{
		pItem= item(j, 1);
		if (pItem)
		{
			pItem->setText("");
			pItem->setData(Qt::ToolTipRole, "0");
			pItem->setData(Qt::UserRole, 0);
			pItem->setData(Qt::WhatsThisRole, "");
		}
	}
}

//	显示一条记录
int PropertyView::OneRowTableDataShow()
{
	QTableWidgetItem	*pItem = NULL;
	QString				ItemText;
	int					j = 0;
	int					FieldNum = 0;
	bool				bColorField = false;
	QVariant			ItemVarData;
	bool				bChangeFlag = false;
	bool				bDeleteFlag = false;
	int					RecordNo = 0;
	FieldNum = m_pTableData->GetCurShowFieldNum();
	if (FieldNum<=0)
	{
		return -1;
	}
	RecordNo = m_pTableData->GetCurSelectRecordNo();

	bool bEdit = false;
	bool bEdit_Once = false;
	for (j=0; j<FieldNum; j++)
	{
		OneFieldDataShow(RecordNo, j);
	}

	return 1;
}

//	显示一条字段（修改字段显示）
int PropertyView::OneFieldDataShow(int RecordNo, int RowNo)
{
	if (RowNo<0 || RowNo >= rowCount() )
	{
		return -1;
	}
	int FieldNo = RowNo;

	QTableWidgetItem	*pItem = NULL;
	QString				ItemText;
	bool				bColorField = false;
	bool				bEdit = false;
	bool				bEdit_Once = false;
	//XBOOL8				bChangeFlag = false;
	unsigned int		ShowFlag = 0;
	QVariant			ItemVarData;
	int					Ret = 0;
	
//	bChangeFlag = false;
	m_pTableData->GetFieldText(RecordNo, FieldNo, ItemVarData, ItemText, bColorField, ShowFlag);

	pItem = NULL;
	pItem = item(FieldNo, 1);
	if (!pItem)
	{
		pItem = new QTableWidgetItem;
		setItem(FieldNo, 1, pItem);
	}

	pItem->setText(ItemText);
	pItem->setData(Qt::ToolTipRole, ItemText);
	pItem->setData(Qt::UserRole, ItemVarData);
	pItem->setData(Qt::WhatsThisRole, ItemText);
	pItem->setData(Qt::TextColorRole, Qt::black);
	pItem->setData(Qt::BackgroundColorRole, Qt::white);
	if ( (ShowFlag&SHOW_FLAG_UNSAVE)>0 )
	{
		pItem->setData(Qt::TextColorRole, Qt::green);
	}
	if ( (ShowFlag&SHOW_FLAG_ERROR)>0 /*|| (ShowFlag&SHOW_FLAG_INCONFORMITY)>0*/)
	{
		pItem->setData(Qt::BackgroundColorRole, Qt::red);
	}

	if (bColorField)
	{
		pItem->setData(Qt::DecorationRole,QColor(QRgb(ItemVarData.toUInt())));
	}

	m_pTableData->IsCurFieldEnableEdit(FieldNo, bEdit, bEdit_Once);
	if (!bEdit)
	{
		if (bEdit_Once && m_pTableData->IsCurRecordNewInsertFlag(RecordNo))
		{
			pItem->setFlags(Qt::ItemIsSelectable | 
							Qt::ItemIsEditable | 
							Qt::ItemIsDragEnabled |
							Qt::ItemIsDropEnabled |
							Qt::ItemIsUserCheckable |
							Qt::ItemIsEnabled |
							Qt::ItemIsTristate);
		}else
		{
			pItem->setFlags(Qt::ItemIsSelectable |
							Qt::ItemIsDragEnabled |
							Qt::ItemIsEnabled |
							Qt::ItemIsTristate);
		}
	}else
	{
		pItem->setFlags(Qt::ItemIsSelectable | 
						Qt::ItemIsEditable | 
						Qt::ItemIsDragEnabled |
						Qt::ItemIsDropEnabled |
						Qt::ItemIsUserCheckable |
						Qt::ItemIsEnabled |
						Qt::ItemIsTristate);
	}

	return 1;
}



//	ITEM用户数据改变
void PropertyView::OnItemUserDataChang(int rowNo, bool bReInit)
{
	if (bReInit)
	{
		emit Signal_ReInit();
	}else
	{
		if (rowNo>=0 && rowNo<rowCount())
		{
			int CurSeltRecNo = m_pTableData->GetCurSelectRecordNo();
			
			if (CurSeltRecNo>=0)
			{
				OneFieldDataShow(CurSeltRecNo, rowNo);
				emit Signal_UserDataChang(CurSeltRecNo%TABLE_PAGE_ROW_NUM, rowNo);
			}
		}
	}
}


//	数据刷新
void PropertyView::OnRefreshData()
{

}

//	重新设置数据显示 不删除ITEM
void PropertyView::OnReSetDataShow(int RecId1, int RecId2, int RecId3, int FieldNo)
{

	return ;
}


